package com.eshealthy.controller;

import com.eshealthy.entity.Department;
import com.eshealthy.utils.EsDrug;
import com.eshealthy.utils.VerifyCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("user")
@Slf4j
public class CommonController {
    /*
     * 生成验证码
     * */
    @GetMapping("getImage")
    public String getImagecode(HttpServletRequest request)throws IOException {
        //1.使用工具类生成验证码
        String code = VerifyCodeUtils.generateVerifyCode(4);
        //2.将验证码放入servletContext作用域
        request.getServletContext().setAttribute("code",code);
        //3.将图片转为base64
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        VerifyCodeUtils.outputImage(220,60,byteArrayOutputStream,code);
        String s = Base64Utils.encodeToString(byteArrayOutputStream.toByteArray());
        return "data:image?png;base64,"+Base64Utils.encodeToString(byteArrayOutputStream.toByteArray());
    }
    @GetMapping("findDrug")
    public Map<String, Object>findDrug() throws IOException {
        HashMap<String,Object> map=new HashMap<>();
        EsDrug esDrug=new EsDrug();
//        map.put("drugs",esDrug.drugList());
        return map;
    }
}
